﻿using System.Net;
using Newtonsoft.Json;
using RestSharp;
using xClient.Framework.StaticResource;
using xClient.Framework;
using System;
using System.Collections.Generic;
using System.IO;

namespace cMES1006.Services
{
   public class ServiceMethod
    {
        public static RestClient _restClient = new RestClient(StaticDictionary.Dictionary.ServiceUrl);

        public static ExecutionResult<dynamic> GetLineBaseInfoList(string vLine=null,string vShift = null)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/line/getline";
            try
            {
                var _request = new RestRequest(api_path, Method.GET);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                if (!string.IsNullOrEmpty(vLine))
                {
                    _request.AddQueryParameter("line", vLine);
                }
               
                if(!string.IsNullOrEmpty(vShift))
                {
                    _request.AddQueryParameter("shift", vShift);
                }
                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "获取线别信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> AddLineBase(Dictionary<string, object> vModel)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/line/add";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);

                var dataModel = JsonConvert.SerializeObject(vModel);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                _request.AddParameter("MODEL", dataModel);
                //if (vModel.ContainsKey("linemanager"))
                //{
                //    Dictionary<string, object> lineMngr = (Dictionary<string, object>)vModel["linemanager"];
                //    if (lineMngr.ContainsKey("manager_picture")&&
                //        !String.IsNullOrEmpty(lineMngr["manager_picture"].ToString()))
                //    {
                //        var fileInfo = new FileInfo(lineMngr["manager_picture"].ToString());
                //        if (fileInfo.Exists)
                //        {
                //            _request.AddFile(fileInfo.Name, fileInfo.FullName);
                //        }
                //    }
                //    if (lineMngr.ContainsKey("leader_picture") &&
                //       !String.IsNullOrEmpty(lineMngr["leader_picture"].ToString()))
                //    {
                //        var fileInfo = new FileInfo(lineMngr["leader_picture"].ToString());
                //        if (fileInfo.Exists)
                //        {
                //            _request.AddFile(fileInfo.Name, fileInfo.FullName);
                //        }
                //    }
                //    //添加Line Name参数
                //    _request.AddParameter("file_type", vModel["line_name"].ToString());
                //}

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "新增线别信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> UpdateLineBase(Dictionary<string, object> vModel)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/basicdata/line/update";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);

                var dataModel = JsonConvert.SerializeObject(vModel);
                _request.AddParameter("MODEL", dataModel);

                //if (vModel.ContainsKey("linemanager"))
                //{
                //    Dictionary<string, object> lineMngr = (Dictionary<string, object>)vModel["linemanager"];
                    
                //    if (lineMngr.ContainsKey("manager_picture") &&
                //        !String.IsNullOrEmpty(lineMngr["manager_picture"].ToString()) &&
                //        !lineMngr["manager_picture"].ToString().ToUpper().StartsWith("HTTP:"))
                //    {
                //        var fileInfo = new FileInfo(lineMngr["manager_picture"].ToString());
                //        if (fileInfo.Exists)
                //        {
                //            _request.AddFile("manager_picture", fileInfo.FullName);
                //        }
                //    }
                //    if (lineMngr.ContainsKey("leader_picture") &&
                //       !String.IsNullOrEmpty(lineMngr["leader_picture"].ToString()) &&
                //       !lineMngr["leader_picture"].ToString().ToUpper().StartsWith("HTTP:"))
                //    {
                //        var fileInfo = new FileInfo(lineMngr["leader_picture"].ToString());
                //        if (fileInfo.Exists)
                //        {
                //            _request.AddFile("leader_picture", fileInfo.FullName);
                //        }
                //    }
                //    //添加Line Name参数
                //    _request.AddParameter("file_type", vModel["line_name"].ToString());
                //}

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "更新线别信息异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> DeleteLineBase(string vId)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = $"/basicdata/line/delete/{vId}";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);                

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas;
                    //result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "删除线别信息异常." + ex.Message;
            }

            return result;
        }


        public static ExecutionResult<dynamic> UploadFileToServer(string vFile, string vFileType = null)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/fileupload/file/upload";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);

                _request.AddParameter("file_type", String.IsNullOrEmpty(vFileType) ? "ESOP" : vFileType);

                System.IO.FileInfo _tmpFile = new System.IO.FileInfo(vFile);
                if (_tmpFile.Exists)
                {
                    _request.AddFile(_tmpFile.Name, _tmpFile.FullName);
                }

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas[0].ToString();
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "上传图片异常." + ex.Message;
            }

            return result;
        }

        public static ExecutionResult<dynamic> DeleteFileFromServer(string vFile)
        {
            var result = new ExecutionResult<dynamic>();
            string api_path = "/fileupload/file/delete";
            try
            {
                var _request = new RestRequest(api_path, Method.POST);
                _request.AddParameter("token", StaticDictionary.Dictionary.Token);
                _request.AddParameter("file_path", vFile);

                IRestResponse _response = _restClient.Execute(_request);
                if (_response.StatusCode == HttpStatusCode.OK)
                {
                    var _content = _response.Content;

                    dynamic _model = JsonConvert.DeserializeObject<dynamic>(_content);

                    result.Status = _model.status;
                    result.Message = _model.message;
                    result.Data = _model.datas[0].ToString();
                    result.ExecuteCount = _model.executeCount;
                }
                else
                {
                    result.Status = false;

                    if (_response.StatusCode == 0)
                    {
                        result.Message = _response.ErrorMessage + ":" + _response.ErrorException;
                    }
                    else
                    {
                        result.Message = "Resource Not Found： " + _response.StatusCode + ":" + _response.StatusDescription + ":";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.Message = "上传图片异常." + ex.Message;
            }

            return result;
        }
    }
}
